package com.jgp.component;

import com.jgp.common.persistence.IdModel;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * 项目   jgp-cloud-parent
 * 作者   loufei
 * 时间   2019-12-29
 */
@Component
public class ListKeyGenerator implements KeyGenerator {
    @Override
    public Object generate(Object target, Method method, Object... params) {
        if(params.length>0&&((List)params[0]).size()>0){
            List<IdModel> list = (List)params[0];
            Set<String> keys = new HashSet<>();
            for (IdModel idModel : list) {
                keys.add(idModel.getId().toString());
            }
            return keys.stream().sorted().collect(Collectors.joining());
        }
        return null;
    }
}
